using System;
using System.Globalization;
using System.Text;
using System.Threading;

namespace ColladaDotNet
{
	public static class DoubleArrayConverter
	{
		public static string ToString(double[] array)
		{
			var sb = new StringBuilder();
			if (array != null && array.Length > 0)
			{
				var d = array[0];
				sb.Append(d.ToString(FormatProvider));
				for (int i = 1; i < array.Length; i++)
				{
					sb.Append(" ");
					d = array[i];
					sb.Append(d.ToString(FormatProvider));
				}
			}
			return sb.ToString();
		}
		public static double[] ToArray(string value)
		{
			if (string.IsNullOrEmpty(value))
				return new double[]{};
			double[] array;
			var l = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			array = new double[l.Length];
				for (int i = 0; i < l.Length; i++)
				{
			try
			{
					array[i] = Parse(l[i]);
			} 
			catch(FormatException ex)
			{
				throw new ApplicationException(ex.Message + " at " + l[i]);
			}
				}
			return array;
		}

		private static double Parse(string l)
		{
			if (l == "-1.#IND")
				return double.NegativeInfinity;
			if (l == "1.#IND")
				return double.PositiveInfinity;

			return double.Parse(l, FormatProvider);
		}

		public static IFormatProvider FormatProvider = CultureInfo.GetCultureInfo("en-US");
	}
	public static class IntArrayConverter
	{
		public static string ToString(int[] array)
		{
			var sb = new StringBuilder();
			if (array != null && array.Length > 0)
			{
				foreach (var d in array)
				{
					sb.Append(d.ToString());
				}
			}
			return sb.ToString();
		}
		public static int[] ToArray(string value)
		{
			int[] array;
			var l = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			array = new int[l.Length];
			for (int i = 0; i < l.Length; i++)
			{
				array[i] = int.Parse(l[i]);
			}
			return array;
		}
	}
	public static class StringArrayConverter
	{
		public static string ToString(string[] array)
		{
			var sb = new StringBuilder();
			if (array != null && array.Length > 0)
			{
				sb.Append(string.Join(" ",array));
			}
			return sb.ToString();
		}
		public static string[] ToArray(string value)
		{
			if (value == null)
				return null;
			return value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
		}
	}
	public static class BoolArrayConverter
	{
		public static string ToString(bool[] array)
		{
			var sb = new StringBuilder();
			if (array != null && array.Length > 0)
			{
				foreach (var d in array)
				{
					sb.Append(d.ToString());
				}
			}
			return sb.ToString();
		}
		public static bool[] ToArray(string value)
		{
			var l = value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			var array = new bool[l.Length];
			for (int i = 0; i < l.Length; i++)
			{
				array[i] = bool.Parse(l[i]);
			}
			return array;
		}
	}
}